package xcmg.device.infra;

import org.apache.commons.collections.CollectionUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by 阿斯顿 on 2018/7/25.
 */
public class ImportUtil{

    /**
     * 导入合并去重
     * repeatList:需要合并的集合；conditionMethodName：进行去重判断的属性名称（首字母大写）；mergeMethodName：需要合并数量的属性名称（首字母大写）
     * totalPrice：合并后零件总金额属性（首字母大写） ；price：零件单价属性（首字母大写）；
     * */
    public static List deRepeat(List repeatList,String conditionMethodName,String mergeMethodName,String totalPrice,String price) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        if(CollectionUtils.isEmpty(repeatList)){
            return repeatList;
        }
        for  ( int i  = 0 ; i< repeatList.size()-1;i++){
            Class listI=repeatList.get(i).getClass();
            Method getMaterialNo=  listI.getDeclaredMethod("get"+conditionMethodName,null);
            Method getAmount=  listI.getDeclaredMethod("get"+mergeMethodName,null);
            BigDecimal sum= new BigDecimal(0);
            try {
                sum = (BigDecimal) getAmount.invoke(repeatList.get(i));
            }catch (ClassCastException e){
              Integer sums= (Integer)getAmount.invoke(repeatList.get(i));
                sum=new BigDecimal(sums);
            }
            for  ( int j=repeatList.size()-1;j>i;j -- )  {
                Class Listj=repeatList.get(j).getClass();
                Method getMaterialNoj=  Listj.getDeclaredMethod("get"+conditionMethodName,null);
                Method getAmountj=  Listj.getDeclaredMethod("get"+mergeMethodName,null);
                BigDecimal sumj=  new BigDecimal(0);
                try {
                    sumj = (BigDecimal) getAmountj.invoke(repeatList.get(j));
                }catch (ClassCastException e){
                    Integer sums= (Integer)getAmount.invoke(repeatList.get(j));
                    sumj=new BigDecimal(sums);
                }
                if  (repeatList.get(j).equals(repeatList.get(i))||getMaterialNoj.invoke(repeatList.get(j)).equals(getMaterialNo.invoke(repeatList.get(i)))){
                    sum=sum.add(sumj);
                    repeatList.remove(j);
                }
            }
            Method setAmount=null;
            try{
                setAmount= listI.getMethod("set"+mergeMethodName,BigDecimal.class);
            }catch (NoSuchMethodException n){
                setAmount= listI.getMethod("set"+mergeMethodName,Integer.class);
            }
            if(totalPrice!=null||price!=null){
                Method setTotalPrice= listI.getMethod("set"+totalPrice,BigDecimal.class);//获取总价的set方法
                Method getPrice= listI.getDeclaredMethod("get"+price);//获取单价的get方法
                BigDecimal prices =(BigDecimal)getPrice.invoke(repeatList.get(i));
                setTotalPrice.invoke(repeatList.get(i),prices==null?new BigDecimal(0):prices.multiply(sum));
            }
            try{
                setAmount.invoke(repeatList.get(i),sum);
            }catch (IllegalArgumentException s){
                setAmount.invoke(repeatList.get(i),(Integer)sum.intValue());
            }

        }
        return repeatList;
    }
}
